﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class DealInfoDAO:BaseService
    {
        public IList<DealInfo> GetDealInfoByGroup(int state,int groupId,int page,int pagesize)
        {
            string hql ="from DealInfo where GroupId="+groupId;
            if (state == 1)
            {
                hql += " and State=1 order by AddDate desc";
            }
            else if (state == 0)
            {
                hql += " and State=0  order by AddDate desc";
            }
            else
            {
                hql += " order by AddDate desc ";
            }
            return FindList<DealInfo>(hql,null, (page - 1) * pagesize, pagesize);
        }
        public IList<DealInfo> GetDealInfoByGroup(int groupId)
        {
            string hql = "from DealInfo where GroupId=" + groupId + " order by AddDate desc ";
          
            return FindList<DealInfo>(hql, null,null,null);
        }
        public int GetCountDealInfoByGroup(int state,int groupId)
        {
            string hql = "select count(*) from DealInfo where GroupId=" + groupId;
            if (state == 1)
            {
                hql += " and State=1 ";
            }
            else if (state == 0)
            {
                hql += " and State=0  ";
            }
            return Convert.ToInt32(GetValue(hql));
        }
        public int GetCountByUserId(int userid)
        {
            return 0;
        }
        public IList<DealInfo> GetAllOrderByUserId(int userid,string key, int pagesize,int pageindex)
        {
            try
            {
                string hql = "from DealInfo where UserId=" + userid;
                if (key != null && key != "")
                {
                    hql += " and (OrderId like '%" + key + "%' or ProductName like '%" + key + "%')";
                }
                return DBHelper.ExecuteHQL(hql + " order by AddDate desc").SetFirstResult((pageindex - 1) * pagesize).SetMaxResults(pagesize).List<DealInfo>();
            }
            catch { return null; }
        }
        public int GetAllOrderCountById(int userid,string key)
        {
            try
            {
                string hql = "from DealInfo where UserId=" + userid;
                if (key != null && key != "")
                {
                    hql += " and (OrderId like '%" + key + "%' or ProductName like '%" + key + "%')";
                }
                return DBHelper.ExecuteHQL(hql + " order by AddDate desc").List<DealInfo>().Count;
            }
            catch { return 0; }
        }
        public bool Delete(int orederid)
        {
            try
            {
                DBHelper.TransDeleteByID("DealInfo", orederid);
                return true;
            }
            catch { return false; }
        }
        public DealInfo GetDealInfoById(int id)
        {
            try
            {
                return DBHelper.ExecuteHQL("from DealInfo where Id=" + id).List<DealInfo>().First();
            }
            catch { return null; }
        }
        /// <summary>
        /// 根据订单号或者产品名称搜索
        /// </summary>
        /// <param name="str"></param>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        public IList<DealInfo> GetSearch(string str, int pagesize, int pageindex)
        {
            try
            {
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("^([\\d]+)");
                if (reg.IsMatch(str))
                {
                    return DBHelper.ExecuteHQL("from DealInfo where OrderId like '%" + str + "%' order by AddDate desc").SetFirstResult((pageindex - 1) * pagesize).SetMaxResults(pagesize).List<DealInfo>();
                }
                else { return DBHelper.ExecuteHQL("from DealInfo where ProductName like '%" + str + "%' order by AddDate desc").SetFirstResult((pageindex - 1) * pagesize).SetMaxResults(pagesize).List<DealInfo>(); }
            }
            catch { return null; }
        }
        public int GetSearchByName(string str) 
        {
            try
            {
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("^([\\d]+)");
                if (reg.IsMatch(str))
                {
                    return DBHelper.ExecuteHQL("from DealInfo where OrderId like '%" + str + "%' order by AddDate desc").List<DealInfo>().Count;
                }
                else { return DBHelper.ExecuteHQL("from DealInfo where ProductName like '%" + str + "%' order by AddDate desc").List<DealInfo>().Count; }
            }
            catch { return 0; }
        }

        #region 个人中心订单查询
        /// <summary>
        /// 订单查询ByState
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        public IList<DealInfo> GetAllOrderByUserId(int userid, int state, int pagesize, int pageindex)
        {
            try
            {
                string hql = "from DealInfo where UserId=" + userid + " and State=" + state;
                
                return DBHelper.ExecuteHQL(hql + " order by AddDate desc").SetFirstResult((pageindex - 1) * pagesize).SetMaxResults(pagesize).List<DealInfo>();
            }
            catch { return null; }
        }
        /// <summary>
        /// 订单查询ByState
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetAllOrderCountById(int userid, int state)
        {
            try
            {
                string hql = "from DealInfo where UserId=" + userid + " and  State=" + state;
               
                return DBHelper.ExecuteHQL(hql + " order by AddDate desc").List<DealInfo>().Count;
            }
            catch { return 0; }
        }
        #endregion

        #region 根据订单号查询单个实体
        /// <summary>
        /// 根据订单号查询单个实体
        /// </summary>
        /// <param name="orderid">订单号</param>
        /// <returns></returns>
        public DealInfo GetDealInfoByOrderId(string orderid)
        {
            try
            {
                return DBHelper.ExecuteHQL("from DealInfo where OrderId='" + orderid+"'").List<DealInfo>().First();
            }
            catch { return null; }
        }
        
        #endregion

        /// <summary>
        /// 查询各种状态下订单的数量
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public int GetCountByState(int id,int p)
        {
            string hql = " from DealInfo where GroupId="+id;
            if (p==0)
            {
                hql += " and State=0";
            }
            else if (p==1)
            {
                hql += " and State=1";
            }
            else if (p==2)
            {
                hql += " and State=2";
            }
            else if (p==-3)
            {
                hql += " ";
            }
            return DBHelper.ExecuteHQL(hql).List<DealInfo>().Count();
        }

        #region 查询订单列表可根据条件查询
        /// <summary>
        /// 查询订单列表可根据条件查询
        /// </summary>
        /// <param name="Condition">查询条件最少要加排序条件</param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<DealInfo> GetDealInfo(string Condition, int page, int pagesize)
        {
            try
            {
                string  hql = "from DealInfo where State!=0 "+Condition ;
             
                return FindList<DealInfo>(hql, null, (page - 1) * pagesize, pagesize);
                
                
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region 查询订单列表条数可根据条件查询
        /// <summary>
        /// 查询订单列表条数可根据条件查询
        /// </summary>
        /// <returns></returns>
        public int GetDealInfo()
        {
            string hql = "select count(*) from DealInfo where State!=0 " ;

            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        #region 批量修改状态
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int UpdateState(string ids, int state)
        {
            int m = 0;
            string hql = "Update from DealInfo set State=" + state + " where Id in (" + ids + ")";
            string hql2 = string.Empty;
            if (state == 0)
            {
                 hql2 = "Update from OrderDetail set State=" + state + " where DealInfoId in (" + ids + ")";
            }
            try
            {
                int n = 0, j = 0;
                if (hql2 != null)
                {
                    n = DBHelper.GetISession.CreateQuery(hql2).ExecuteUpdate();
                }
                j = DBHelper.GetISession.CreateQuery(hql).ExecuteUpdate();

                return m = j;
            }
            catch
            {
                return m;
            }

        }
        #endregion

        #region 根据Id(修改state)
        /// <summary>
        /// 根据Id(修改state)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateStateById(int id,int state)
        {
            string hql = "Update from DealInfo set State=" + state + " where Id=" + id;
            string hql2 = string.Empty;
            int m = 0, n = 0;
            if (state == 0)
            {
                hql2 = "Update from OrderDetail set State=" + state + " where DealInfoId =" + id;
                n = DBHelper.GetISession.CreateQuery(hql2).ExecuteUpdate();
                m = DBHelper.GetISession.CreateQuery(hql).ExecuteUpdate();
                if ((n != 0 && m != 0) || m != 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                m = DBHelper.GetISession.CreateQuery(hql).ExecuteUpdate();
                if (m != 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region 模糊查询list
        /// <summary>
        /// 模糊查询list
        /// </summary>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<DealInfo> GetLike(string key, int page, int pagesize)
        {
            try
            {
                string hql = "";
                if (key == "审核订单" || key == "确认订单" || key == "订单结束" || key == "回款" || key == "退货" || key == "收货" || key == "发货" || key == "取消订单")
                {
                    int state = GetStateByLike(key);
                    hql = " from DealInfo where OrderId like '%" + key + "%' or UserName like '%" + key + "%' or TotalPrice like '%" + key + "%' or AddDate like '%" + key + "%' or EndDate like '%" + key + "%' or TypeCon like '%" + key + "%'or State like '%" + state + "%' order by AddDate desc";
                }
                else
                {
                    hql = " from DealInfo where OrderId like '%" + key + "%' or UserName like '%" + key + "%' or TotalPrice like '%" + key + "%' or AddDate like '%" + key + "%' or EndDate like '%" + key + "%' or TypeCon like '%" + key + "%' and State!=0 order by AddDate desc";
              
                }
                
                return FindList<DealInfo>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region 模糊查询总数
        /// <summary>
        /// 模糊查询总数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int GetLike(string key)
        {
            string hql = "";
            if (key == "审核订单" || key == "确认订单" || key == "订单结束" || key == "回款" || key == "退货" || key == "收货" || key == "发货" || key == "取消订单")
            {
                int state = GetStateByLike(key);
                hql = "select count(*) from DealInfo where OrderId like '%" + key + "%' or UserName like '%" + key + "%' or TotalPrice like '%" + key + "%' or AddDate like '%" + key + "%' or EndDate like '%" + key + "%' or TypeCon like '%" + key + "%'or State like '%" + state + "%' ";
            }
            else
            {
                hql = "select count(*) from DealInfo where OrderId like '%" + key + "%' or UserName like '%" + key + "%' or TotalPrice like '%" + key + "%' or AddDate like '%" + key + "%' or EndDate like '%" + key + "%' or TypeCon like '%" + key + "%' and State!=0 ";
            }
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        #region 取得State
        /// <summary>
        /// 取得State
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int GetStateByLike(string key)
        {
            int i = 1;
            switch (key)
            {
                case "审核订单":
                    i = 1; break;
                case "确认订单":
                    i = 2; break;
                case "取消订单":
                    i = 3; break;
                case "发货":
                    i = 4; break;
                case "收货":
                    i = 5; break;
                case "回款":
                    i = 6; break;
                case "退货":
                    i = 7; break;
                case "订单结束":
                    i = 8; break;
                default :
                    i = 1; break;
            }
            return i;
        }
        #endregion
    }
}
